package utils;

import java.io.File;
import java.io.PrintWriter;

import main.NMFTestProcessFixedR.SummaryInfo;

import org.ejml.simple.SimpleMatrix;

import documents.ReutersDocumentCollection;

public class ClassificationUtil {
   public static ReutersDocumentCollection rDc;
   
   public static SimpleMatrix getPreProcessedMatrix(final String[] fileNames, boolean titleFeatureFlag) {
      final StopWordList swl = new StopWordList();
      final String breakRegEx = "[.[^a-zA-Z]]";
      
      final ReutersDocumentCollection t = new ReutersDocumentCollection(swl.getStopWordSet(), new Stemmer(), breakRegEx);
      t.setFileNames(fileNames);
      t.initialize(titleFeatureFlag);
      rDc = t;
      return TfIdf.doTfIdf(t.getMatrix());
   }
   
   public static void writeToCSVRandom(final SummaryInfo sI[][][], final String fileName) {
      try {
         final PrintWriter writer = new PrintWriter(new File(fileName));
         
         for (final SummaryInfo[][] element : sI) {
            writer.print("," + element[0][0].rValue);
         }
         writer.println();
         
         //0 - Best
         //1 - Worst
         //2 - Average
         final double[][] results = new double[3][sI.length];
         
         int totalAcrossDocs = 1, totalCorrectAcrossDocs, bestCorrect, worstCorrect;
         
         //Writes our results to the results array.
         for (int rIndex = 0; rIndex < sI.length; rIndex++) {
            double sumOfPercentages = 0;
            bestCorrect = -1;
            worstCorrect = 10000000;
            for (int sampleNum = 0; sampleNum < sI[0].length; sampleNum++) {
               totalAcrossDocs = 0;
               totalCorrectAcrossDocs = 0;
               for (int docCol = 0; docCol < sI[0][0].length; docCol++) {
                  totalAcrossDocs += sI[rIndex][sampleNum][docCol].totalNumberOfDocuments;
                  totalCorrectAcrossDocs += sI[rIndex][sampleNum][docCol].numberOfDocumentsClassifiedCorrectly;
               }
               sumOfPercentages += (double) totalCorrectAcrossDocs / totalAcrossDocs;
               if (totalCorrectAcrossDocs > bestCorrect) {
                  bestCorrect = totalCorrectAcrossDocs;
               }
               else if (totalCorrectAcrossDocs < worstCorrect) {
                  worstCorrect = totalCorrectAcrossDocs;
               }
            }
            results[0][rIndex] = (double) bestCorrect / totalAcrossDocs;
            results[1][rIndex] = (double) worstCorrect / totalAcrossDocs;
            results[2][rIndex] = sumOfPercentages / sI[0].length;
         }
         
         writeLine(writer, "Best Random", results[0]);
         writeLine(writer, "Worst Random", results[1]);
         writeLine(writer, "Average Random", results[2]);
         
         writer.close();
         
      }
      catch (final Exception e) {
         System.out.println("I WON'T PLAY YOUR GAME JAVA");
         e.printStackTrace();
      }
   }
   
   public static void writeLine(final PrintWriter wr, final String lName, final double[] values) {
      wr.print(lName + " ");
      for (final double value : values) {
         wr.print("," + value);
      }
      wr.println();
   }
   
   //First dimension - rValue
   //Second dimension - tool
   //Third dimension - document
   public static void writeToCSV(final SummaryInfo sI[][][], final String fileName) {
      try {
         final PrintWriter writer = new PrintWriter(new File(fileName));
         
         for (final SummaryInfo[][] element : sI) {
            writer.print("," + element[0][0].rValue);
         }
         writer.println();
         
         for (int toolNum = 0; toolNum < sI[0].length; toolNum++) {
            writer.print(sI[0][toolNum][0].nmfTool + " ");
            for (final SummaryInfo[][] element : sI) {
               int totalClassCorrect = 0;
               int totalDocs = 0;
               for (int docNum = 0; docNum < sI[0][0].length; docNum++) {
                  totalClassCorrect += element[toolNum][docNum].numberOfDocumentsClassifiedCorrectly;
                  totalDocs += element[toolNum][docNum].totalNumberOfDocuments;
               }
               
               writer.print("," + (double) totalClassCorrect / totalDocs);
            }
            writer.println();
         }
         writer.close();
      }
      catch (final Exception e) {
         System.out.println("I WON'T PLAY YOUR GAME JAVA");
         e.printStackTrace();
      }
   }
   
   public static void writeTime(final String fileName) {
      
   }
}
